

import 'package:flutter/material.dart' ;
import 'package:flutter/gestures.dart' ;






void main() {
  runApp(MyApp());
}

class MyApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      home: Scaffold(
        appBar: AppBar(
          title: Text('Text Width Example'),
        ),
        body: MyTextWidget(),
      ),
    );
  }
}
class MyTextWidget extends StatefulWidget {
  @override
  State<StatefulWidget> createState() {
    return MyTextWidgetState();
  }

}
class MyTextWidgetState extends State<MyTextWidget> {

  @override
  void initState() {

    super.initState();
  }

  @override
  Widget build(BuildContext context) {
    return GestureDetector(
      onHorizontalDragUpdate: (_){
        print("外层触发拖动");
      },
      child: Container(
        padding: EdgeInsets.all(20),
        color: Colors.blue,
        child: OnlyOneDirectionDragRecognizerWidget(
          child: Container(
            color: Colors.red,
            width: 300,
            height: 300,
          ),
        ),
      ),
    );
  }
}


class OnlyOnePointerRecognizer extends HorizontalDragGestureRecognizer {
  ///初始化拖拽方向大于0还是小于0
  double? _direction;
  @override
  void handleEvent(PointerEvent event) {
    print("handleEvent==============${event.delta.dx}");

    if (event.delta.dx != 0 && _direction == null) {
      print("rejected==============${event.delta.dx}");
      _direction = event.delta.dx;
      if (event.delta.dx < 0) {
        print("rejected==============${event.delta.dx}");

        ///一开始<0 就rejected掉 会触发外层拖动 外层没准备好会报这个 但是能正常log 'package:flutter/src/gestures/monodrag.dart': Failed assertion: line 396 pos 12: '_state != _DragState.ready': is not true.
        resolve(GestureDisposition.rejected);
      }
    }
    super.handleEvent(event);
  }
}

class OnlyOneDirectionDragRecognizerWidget extends StatelessWidget {
  final Widget? child;
  OnlyOneDirectionDragRecognizerWidget({ this.child,});

  @override
  Widget build(BuildContext context) {
    Map<Type, GestureRecognizerFactory> gestures = {
      OnlyOnePointerRecognizer: GestureRecognizerFactoryWithHandlers<OnlyOnePointerRecognizer>(
              () => OnlyOnePointerRecognizer(),
              (OnlyOnePointerRecognizer instance) {
            instance..onUpdate = (_){
              print("内层触发拖动");
            };
          }
      )
    };
    return RawGestureDetector(
        gestures: gestures,
        child: child
    );
  }
}

